<?php
    include_once("../inc/connection.php");
    include_once("../inc/constants.inc.php");
    $typeId = mysql_real_escape_string($_GET['typeId']);
    $student_id = $_GET['student_id'];
    $timer="SELECT * FROM `timer` where student_id='$student_id' and type_id='$typeId'";
	$resTimer = mysql_query($timer);
    $rowTimer = mysql_fetch_array($resTimer);
	$time_spent = 0;
	$time=time();
	//$milliseconds = round(microtime(true) * 1000);
	$time_spent_millisecond = 0;
	if(isset($rowTimer['start_time']) && !empty($rowTimer['start_time']))
	{
		$time_spent = time() - $rowTimer['start_time'];
		$time_spent_millisecond = $time_spent * 1000;
	}else{	
		//$sql = "UPDATE `timer` SET start_time='".time()."' student_id='$student_id' and type_id='$typeId'";
		$time = time();
		$sql = "INSERT INTO `timer`(`student_id`, `type_id`, `start_time`, `end_time`) VALUES ($student_id,$typeId,$time,'0')";
		mysql_query($sql);
	}

    $examTime="SELECT examTime FROM `exam_config` where typeId='$typeId'";
    $rec = mysql_query($examTime);
    $res = mysql_fetch_array($rec);
	$timeRemaining = $res['examTime'] - $time_spent_millisecond;
    echo $timeRemaining;
?>